package com.ktjiaoyu.server.controller;

import com.ktjiaoyu.server.domain.dto.LoginDto;
import com.ktjiaoyu.server.service.AdminService;
import com.ktjiaoyu.server.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "认证相关接口")
@RestController
public class LoginController {

    @Autowired
    private AdminService adminService;

    @ApiOperation(value = "登录接口",notes = "详细说明。。。。")
    @PostMapping("/doLogin")
    public ResponseResult doLogin(@RequestBody LoginDto loginDto){
        // 检查验证码
        if(!adminService.checkCaptcha(loginDto.getUuid(),loginDto.getCaptcha())){
            return ResponseResult.error("验证码错误");
        }

        String token = adminService.login(loginDto);

        ResponseResult responseResult = ResponseResult.success("登录成功");
        responseResult.put("token", token);

        return responseResult;
    }

    @ApiOperation(value = "登出接口",notes = "详细说明。。。。")
    @PostMapping("/doLogout")
    public ResponseResult doLogout(){
        adminService.logout();
        return ResponseResult.success();
    }
}
